<template>
  <div>
    <div class="mall-main" style="margin: 0 auto">
      <div class="item-list">
        <MallList :list="itemList" v-if="itemList.length > 0"></MallList>
        <div v-else class="none-item-list">
          抱歉没有找到相关的商品，请看看其他的商品
        </div>
      </div>
      <div style="text-align: center">
        <el-pagination @current-change="handleCurrentChange" :page-size="size" :total="total" background layout="prev, pager, next"></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import MallList from '@/components/MallList'
import { mixin } from '../mixins'
import { SPACE, selectPage, selectPageByForeignId, selectPageByWord } from '@/api/baseApi'
export default {
  mixins: [mixin],
  components: {
    MallList
  },
  data() {
    return {
      itemTypeList: '', // 分类列表
      itemList: '', // 商品列表
      size: 15, // 每页显示的商品数量
      activeName: '-1', // 分类列表当前选中的id
      name: ''
    }
  },
  activated() {
    // 如果路由传递了typeId，则显示对应的分类商品
    if (this.$route.query.typeId !== undefined) {
      this.findItemByType()
      return
    }
    // 如果路由传递了search，则为搜索，显示对应的分类商品
    if (this.$route.query.search !== undefined) {
      this.keyWord = this.$route.query.search
    }
    if (this.$route.query.activeName !== undefined) {
      this.activeName = this.$route.query.activeName
    }
  },
  watch: {
    // 监听搜索条件，响应相应的商品
    search: function (val) {
      if (val !== '') {
        console.log('search-val: ' + val)
        selectPageByWord(SPACE.item, this.current, this.size, val).then(res => {
          this.current = 1
          this.itemList = res.state === 200 ? res.data.records : []
        })
      }
    },
    // 监听路由变化，更新路由传递了搜索条件
    $route: function (val) {
      if (val.path === '/item') {
        if (val.query.typeId !== undefined) {
          this.current = 1
          this.findItemByType()
        }
        if (val.query.search !== undefined) {
          this.activeName = '-1'
          this.current = 1
          this.keyWord = val.query.search
          this.findRow()
        }
      }
    }
  },
  methods: {
    // 查询无条件的商品列表
    async findRow() {
      if (this.keyWord === '') {
        selectPage(SPACE.item, this.current, this.size).then(res => {
          if (res.state === 200) {
            this.itemList = res.data.records
            this.total = res.data.total
          }
        })
      } else {
        selectPageByWord(SPACE.item, this.current, this.size, this.keyWord).then(res => {
          if (res.state === 200) {
            this.itemList = res.data.records
            this.total = res.data.total
          }
        })
      }
    },
    // 根据类别查询商品列表
    async findItemByType() {
      selectPageByForeignId(SPACE.item, this.current, this.size, this.$route.query.typeId).then(res => {
        if (res.state === 200) {
          this.itemList = res.data.records
          this.total = res.data.total
        }
      })
    }
  }
}
</script>

<style scoped>
.mall-main .item-list {
  min-height: 650px;
  padding-top: 14.5px;
  margin-left: -13.7px;
  overflow: auto;
}
.mall-main .none-item-list {
  color: #333;
  margin-left: 13.7px;
}
</style>
